﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QLTV
{
    public partial class frmTraCuuSach : Form
    {
        public frmTraCuuSach()
        {
            InitializeComponent();
        }

        private void frmTraCuuSach_Load(object sender, EventArgs e)
        {
            List<TheLoaiDTO> listTL = new List<TheLoaiDTO>();
            listTL = TheLoaiService.LayDanhSachTheLoai();
            cmbTheLoai.ValueMember = TheLoaiColumns.MaTheLoai;
            cmbTheLoai.DisplayMember = TheLoaiColumns.TenTheLoai;
            cmbTheLoai.DataSource = listTL;

            List<NhaXuatBanDTO> ListNXB = new List<NhaXuatBanDTO>();
            ListNXB = NhaXuatBanServices.LayDanhSachNXB();
            cmbNhaXB.ValueMember = NhaXuabanColumns.MaNhaXuatBan;
            cmbNhaXB.DisplayMember = NhaXuabanColumns.TenNhaXuatBan;
            cmbNhaXB.DataSource = ListNXB;

            txtMaSoSach.Enabled = false;
            txtNamXB.Enabled = false;
            txtTacGia.Enabled = false;
            txtTenSach.Enabled = false;
            cmbNhaXB.Enabled = false;
            cmbTheLoai.Enabled = false;
        }        

        private void btnTraCuu_Click(object sender, EventArgs e)
        {
            string maSoSach="";
            string tenSach="";
            string tenTacGia="";
            int maTheLoai= 0;
            int maNXB=0;
            int namXB= 0;            
            DataTable BangSach = new DataTable();

            if (checkMaSoSach.Checked == true)           
                 maSoSach = txtMaSoSach.Text.Trim();
            
            if (checkTenSach.Checked == true)            
                
                tenSach = txtTenSach.Text.Trim();
            
            if (checkTacGia.Checked == true)          
                
                tenTacGia = txtTacGia.Text.Trim();
           
            if (checkTheLoai.Checked == true)
            {                
                cmbTheLoai.DropDownStyle = ComboBoxStyle.DropDownList;
                maTheLoai = Convert.ToInt32(cmbTheLoai.SelectedValue);
            }
            if (checkNhaXB.Checked == true)
            {                
                cmbNhaXB.DropDownStyle = ComboBoxStyle.DropDownList;
                maNXB = Convert.ToInt32(cmbNhaXB.SelectedValue);
            }
            if (checkNamXB.Checked == true)          
                
                namXB = Convert.ToInt32(txtNamXB.Text);
           
            BangSach = SachService.TraCuuSach(maSoSach, tenSach, tenTacGia, maTheLoai, maNXB, namXB);
            
            dgvKetQuaTraCuu.DataSource = BangSach;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkMaSoSach_CheckedChanged(object sender, EventArgs e)
        {
            if (checkMaSoSach.Checked == true)
                txtMaSoSach.Enabled = true;
            else
                txtMaSoSach.Enabled = false;
            
        }

        private void checkTenSach_CheckedChanged(object sender, EventArgs e)
        {
            if (checkTenSach.Checked == true)
                txtTenSach.Enabled = true;
            else
                txtTenSach.Enabled = false;           
        }

        private void checkTacGia_CheckedChanged(object sender, EventArgs e)
        {
            if (checkTacGia.Checked == true)
                txtTacGia.Enabled = true;
            else
                txtTacGia.Enabled = false;           
        }

        private void checkTheLoai_CheckedChanged(object sender, EventArgs e)
        {
            if (checkTheLoai.Checked == true)
            {
                cmbTheLoai.Enabled = true;
                cmbTheLoai.DropDownStyle = ComboBoxStyle.DropDownList;
            }
            else
                cmbTheLoai.Enabled = false;
        }

        private void checkNhaXB_CheckedChanged(object sender, EventArgs e)
        {
            if (checkNhaXB.Checked == true)
            {
                cmbNhaXB.Enabled = true;
                cmbNhaXB.DropDownStyle = ComboBoxStyle.DropDownList;
            }
            else
                cmbNhaXB.Enabled = false;
        }

        private void checkNamXB_CheckedChanged(object sender, EventArgs e)
        {
            if (checkNamXB.Checked == true)
                txtNamXB.Enabled = true;
            else
                txtNamXB.Enabled = false;
            
        }
    }
}
